// pages/module/liveManagement/liveManagement.js
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import shareFun from '../../../utils/shareFun.js';
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import {
  checkLiveList,
  getHostInfo,
  
} from '../../../utils/imapi.js';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_cancle: false,
    title: '我的直播间',
    roomId: '',
    live_cont: '欢迎来到星品生活直播间',
    hostInfo: {
      wx_headimgurl: "https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83ep6NWzD4ib8LWonlZw30gDiaguoNibakF7FrqaUZ9CUt3yBXRibMQKHxlicvgj6OgYgPbem6pwr1eyphTQ/132",
      title: "星品",
      count: 10,
      line_num: 100000,
      fans: 888,
      is_attention: 1
    },
    zIndex: true,
    is_edit: false,
    liveList: [],
    page_index:0,
    page_size:10,
    img_list: "img_list.img_list",
    status:"1"


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData({
      img_list: img_list.img_list
    })
    shareFun.hidenShareMenu()

    
    this.setData({
      userInfo: app.globalData.userInfo,
    })

  },
  async getHostInfoHandler(){

    const { data } = await getHostInfo()
    this.setData({
      hostInfo: data,
    })
  },

  async getList(page_index) {
   
    const liveList = await checkLiveList({ room_id: this.data.hostInfo.room_id,page_index:page_index ,status:this.data.status})

    this.setData({
      liveList: this.data.liveList.concat(liveList.data),
      
      
      page_index:page_index,
    })
  },
  createHost() {
    wx.navigateTo({
      url: '/pages/module/createLive/createLive',
    })
  },
  proManagement() {
    wx.navigateTo({
      url: '/pages/module/proManagement/proManagement',
    })
  },
  goMInfo() {
    wx.navigateTo({
      url: '/pages/module/merchantInfo/merchantInfo',
    })
  },
  prompt(){
	  wx.showModal({
	    title: '提示',
	    content: '进入PC版 商品管理、订单管理、财务中心',
	    success: function () {
	      
	    }
	  })
  },
  liveDetail(e) {  // 通过 liveBox 组件 触发detail事件
    var id = e.detail.id;
    var status = e.detail.status
    let roomId = [id]
    let customParams = encodeURIComponent(JSON.stringify({ sence: app.globalData.userInfo.id, from: 'live' }))
    let BaseUrl = app.globalData.is_test ? 'wx2b03c6e691cd7370' : 'api.comm.xingkonglian.net';
    if (status == 102 || status == 101 || status == 105 || status == 106) {
      // 主播端 进入直播
      wx.navigateToMiniProgram({
        appId: 'wxcbbd86b156ae4441',
      })
    } else {
      // 粉丝端：进入直播间
      wx.navigateTo({
        url: `plugin-private://${BaseUrl}/pages/live-player-plugin?room_id=${roomId}&custom_params=${customParams}`
      })
    }
  },
  edit() {
    this.setData({
      is_edit: true,
    })
  },
  blur() {
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '是否确认修改主播介绍',
      success: function () {
        that.setData({
          is_edit: false,
        })
      }
    })
  },
  showCancle() {
    this.setData({
      show_cancle: true,
    })
  },
  cancleAttention() {

  },
  cancle() {
    this.setData({
      show_cancle: false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    // const {data} = await getHostInfo()
    // const liveList = await checkLiveList({room_id:data.room_id})
    // this.setData({
    //   liveList:liveList.data,
    //   userInfo:app.globalData.userInfo,
    //   hostInfo:data
    // })

    console.log('----liveManagement.onShow ')

    await this.getHostInfoHandler()
    this.getList(this.data.page_index+1)


  },
  load_valid_lives(){ //加载有效的直播

     this.setData({
       status:"1",
       liveList:[],
       page_index:0,
     })

     this.getList(this.data.page_index+1)

     
  },

  load_invalid_lives(){ //加载无效的直播

    this.setData({
      status:"0",
      liveList:[],
      page_index:0,
    })
    this.getList(this.data.page_index+1)


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(() => {
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 500);

    this.getHostInfoHandler()

    this.setData({
      page_index:1,
      liveList:[]
    })
    this.getList(this.data.page_index)
  },


  reload:function(){
    this.getHostInfoHandler()
    this.setData({
      page_index:1,
      liveList:[]
    })
    this.getList(this.data.page_index)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    this.getHostInfoHandler()
    this.getList(this.data.page_index+1)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})